package org.layuiframework.generator.engine;

import org.layuiframework.generator.config.po.LayuiTableField;
import org.layuiframework.generator.engine.impl.*;

import java.util.ArrayList;
import java.util.List;

public class TableFieldConverters {

    private List<TableFieldConverter> defaultConverters;

    public static void convert(LayuiTableField layuiTableField, String comment){
        TableFieldConverter  converter = null;
        for (TableFieldConverter tableFieldConverter: getInstance().defaultConverters) {
            if(tableFieldConverter.canDeal(comment)) converter = tableFieldConverter;
        }
        if(null != converter){
            converter.deal(layuiTableField, comment);
        }else {
            layuiTableField.setUnit(comment);
        }
    }

    private static TableFieldConverters getInstance(){
        return Singleton.getInstance();
    }

    private void init(){
        defaultConverters= new ArrayList<>();
        defaultConverters.add(new TableFieldConvertId());
        defaultConverters.add(new TableFieldConvertName());
        defaultConverters.add(new TableFieldConvertRadio());
        defaultConverters.add(new TableFieldConvertSelect());
        defaultConverters.add(new TableFieldConvertTime());
        defaultConverters.add(new TableFieldConvertVerify());
    }

    private TableFieldConverters (){
        init();
    }

    private static class Singleton{

        private static TableFieldConverters singleton;

        private static TableFieldConverters getInstance(){

            if(null == singleton){
                singleton = new TableFieldConverters();
            }
            return singleton;

        }
    }

}
